USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_CUOTAS]    Script Date: 11/24/2011 11:35:38 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('WASP_M0013_CABECERA_CUOTAS') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[WASP_M0013_CABECERA_CUOTAS]
GO

USE [BMIG_Mensual_POST_CADENA]
GO

/****** Object:  StoredProcedure [dbo].[WASP_M0013_CABECERA_CUOTAS]    Script Date: 11/24/2011 11:35:38 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[WASP_M0013_CABECERA_CUOTAS] (@FECHA_INICIO CHAR(8), @FECHA_TERMINO AS CHAR(8)) AS

BEGIN
    SELECT  a.ppsuc
          , a.ppcta
          , a.ppoper
          , a.ppsbop
          , SUM(b.ppcap + b.ppint) AS total_credito
          , COUNT(b.ppfvto) AS totcuotas
    into  dbo.cabecera_produccion_Cuotas      
    FROM  dbo.fsd611 AS a WITH (nolock) 
          CROSS JOIN dbo.fsd601 AS b WITH (nolock) 
          CROSS JOIN dbo.fsd010 AS c WITH (nolock) 
          CROSS JOIN dbo.cabecera_seguro_asisten_penIP AS d 
          CROSS JOIN dbo.JT73109 AS e WITH (nolock)
    WHERE (a.ppsuc = b.ppsuc) 
          AND (a.ppcta = b.ppcta) 
          AND (a.ppoper = b.ppoper) 
          AND (c.aosbop = 0) 
          AND (a.pptipo = '') 
          AND (a.ppexte = 0) 
          AND (a.pgcod = b.pgcod) 
          AND (a.ppmod = b.ppmod) 
          AND (a.ppmda = b.ppmda) 
          AND (a.ppoper = c.aooper) 
          AND (a.ppcta = c.aocta) 
          AND (a.ppsuc = c.aosuc) 
          AND (a.ppsbop = b.ppsbop) 
          AND (a.pptope = b.pptope) 
          AND (a.ppsuc = b.ppsuc) 
          AND (a.ppmda = b.ppmda) 
          AND (a.ppcta = b.ppcta) 
          AND (a.ppoper = b.ppoper) 
          AND (a.ppsuc = d.ppsuc) 
          AND (a.ppcta = d.ppcta) 
          AND (a.ppoper = d.ppoper) 
          AND (b.d601co = 'S') 
          AND (e.JT73109FPA = a.ppoper) 
          AND (e.JT73109SUC = a.ppsuc) 
          AND (e.JT73109FAP BETWEEN @FECHA_INICIO AND @FECHA_TERMINO)
    GROUP BY a.ppsuc, a.ppcta, a.ppoper, a.ppsbop
    ORDER BY a.ppcta
END
GO


